package com.atguigu.dga.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * @Description: 通过SpringIOC容器创建类
 * @Author: lay
 * @Date: 2024/6/26 19:34
 */
@Component
public class SpringIOCProvider implements ApplicationContextAware {

    private ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    /**
     * 通过名字从容器中获取Bean对象
     * @param beanName 类名
     * @param classType 类型
     * @return
     * @param <T>
     */
    public <T> T getByName(String beanName, Class<T> classType) {
        return applicationContext.getBean(beanName, classType);
    }
}
